package com.rock.platform.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rock.platform.entity.GridParamsEntity;
import com.rock.platform.entity.FilterEntity;
import com.rock.platform.entity.SortEntity;

public class SqlConstructor {

	private Map<String, Object> filterParams = new HashMap<String,Object>();
	private StringBuffer _sbSql = new StringBuffer();
	private String sqlNoSort;
	private Class<?> entityType;
	
	/**
	 * Sql构造器,拼接过滤条件，排序字段
	 * @param sql 原始Sql
	 * @param gridParams action获取的request参数，包括了 grid 过滤,排序
	 * @param entityType
	 */
	public SqlConstructor(String sql,GridParamsEntity gridParams, Class<?> entityType)
	{
		copySql(sql);
		if (gridParams.getFilterList().size() > 0)
		{
			setEntityType(entityType);
			filterConstruction(gridParams.getFilterList());
		}
		if (gridParams.getSortList().size() > 0)
		{
			setEntityType(entityType);
			sortConstruction(gridParams.getSortList());
		}
	}
	
	private void copySql(String sql)
	{
		this.sqlNoSort = sql;
		_sbSql.append(" select * from ( ");
		_sbSql.append(sql);
		_sbSql.append(" ) t ");
	}
	
	private void filterConstruction(List<FilterEntity> filterEntity)
	{
		//Filters for Strings, Numeric Ranges, Date Ranges, Lists, and Boolean are the standard filters available.
		_sbSql.append(" where 1=1 ");
		for(FilterEntity e : filterEntity)
		{
			String type = e.getType().toLowerCase();
			Object value = e.getValue();
			String operator = " = ";
			String filterParams = " :filter" + e.getField();
			
			if (type.equals("string")) 
			{
				operator = " like ";
				value = "%"+e.getValue()+"%";
			} 
			if (type.equals("numeric") || type.equals("date")) 
			{
				operator = e.getComparison();
			} 
			if (type.equals("list"))
			{
				operator = " in ";
				filterParams = " ( " + filterParams + " ) ";
			}
			
			_sbSql.append(" and ");
			_sbSql.append(e.getField());
			_sbSql.append(operator);
			_sbSql.append(filterParams);
			
			this.filterParams.put("filter" + e.getField(), value);
		}
		this.sqlNoSort = _sbSql.toString();
	}
	private void sortConstruction(List<SortEntity> sortEntity)
	{
		int iSize = sortEntity.size();
		if (iSize > 0) {
			_sbSql.append(" order by ");
			for(int i = 0; i < sortEntity.size(); i++)
			{
				 SortEntity e = sortEntity.get(i);
				_sbSql.append(e.getField());
				_sbSql.append(" ");
				_sbSql.append(e.getDirection());
				if (i < iSize - 1) {
					_sbSql.append(", " );
				}
			}
		}
	}
	
	/**
	 * 获取构造后的完整SQL
	 * @return
	 */
	public String getSql() {
		return _sbSql.toString();
	}

	/**
	 * 获取构造后的无排序片段的Sql
	 * @return
	 */
	public String getSqlNoSort() {
		return this.sqlNoSort;
	}
	
	/**
	 * 获取构造后的Sql参数Map
	 * @return
	 */
	public Map<String, Object> getFilterParams() {
		return filterParams;
	}

	private void setEntityType(Class<?> entityType) {
		if (this.entityType == null)
		{
			this.entityType = entityType;
		}
	}

}
